class_name ManaUI
extends Panel

@export var char_stats: CharacterStats : set = _set_char_stats

@onready var mana_label: Label = $ManaLabel


func _set_char_stats(value: CharacterStats) -> void:
	char_stats = value
	
	if not char_stats.stats_changed.is_connected(_on_stats_changed):
		char_stats.stats_changed.connect(_on_stats_changed)
	
	if not is_node_ready():
		await ready
	
	_on_stats_changed()


func _on_stats_changed() -> void:
	var available_mana = char_stats.get_available_mana()
	var max_mana = char_stats.max_mana
	
	# 如果有贷款影响，显示额外信息
	if char_stats.old_loan_mana > 0:
		print("🔧 ManaUI._on_stats_changed: 法力值变化 - %d/%d (贷款影响: -%d)" % [available_mana, max_mana, char_stats.old_loan_mana])
		mana_label.text = "%d/%d" % [available_mana, max_mana]
	else:
		print("🔧 ManaUI._on_stats_changed: 法力值变化 - %d/%d" % [available_mana, max_mana])
		mana_label.text = "%d/%d" % [available_mana, max_mana]
